package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.employee.KSalarySetupBo;
import com.ruoyi.wms.domain.entity.employee.KSalarySetup;
import com.ruoyi.wms.domain.vo.KSalarySetupVo;
import com.ruoyi.wms.mapper.KSalarySetupMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * k指标设置Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-11-27
 */
@RequiredArgsConstructor
@Service
public class KSalarySetupService {

    private final KSalarySetupMapper kSalarySetupMapper;

    /**
     * 查询k指标设置
     */
    public KSalarySetupVo queryById(Long id){
        return kSalarySetupMapper.selectVoById(id);
    }

    /**
     * 查询k指标设置列表
     */
    public TableDataInfo<KSalarySetupVo> queryPageList(KSalarySetupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KSalarySetup> lqw = buildQueryWrapper(bo);
        Page<KSalarySetupVo> result = kSalarySetupMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询k指标设置列表
     */
    public List<KSalarySetupVo> queryList(KSalarySetupBo bo) {
        LambdaQueryWrapper<KSalarySetup> lqw = buildQueryWrapper(bo);
        return kSalarySetupMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KSalarySetup> buildQueryWrapper(KSalarySetupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KSalarySetup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMetricName()), KSalarySetup::getMetricName, bo.getMetricName());
        lqw.eq(bo.getWeight() != null, KSalarySetup::getWeight, bo.getWeight());
        lqw.eq(bo.getBalancePoint() != null, KSalarySetup::getBalancePoint, bo.getBalancePoint());
        lqw.eq(bo.getRewardScale() != null, KSalarySetup::getRewardScale, bo.getRewardScale());
        lqw.eq(bo.getReward() != null, KSalarySetup::getReward, bo.getReward());
        lqw.eq(bo.getPunishmentScale() != null, KSalarySetup::getPunishmentScale, bo.getPunishmentScale());
        lqw.eq(bo.getPunishment() != null, KSalarySetup::getPunishment, bo.getPunishment());
        return lqw;
    }

    /**
     * 新增k指标设置
     */
    public void insertByBo(KSalarySetupBo bo) {
        KSalarySetup add = MapstructUtils.convert(bo, KSalarySetup.class);
        kSalarySetupMapper.insert(add);
    }


    /**
     * 修改k指标设置
     */
    public void updateByBo(KSalarySetupBo bo) {
        KSalarySetup update = MapstructUtils.convert(bo, KSalarySetup.class);
        kSalarySetupMapper.updateById(update);
    }

    /**
     * 批量删除k指标设置
     */
    public void deleteByIds(Collection<Long> ids) {
        kSalarySetupMapper.deleteBatchIds(ids);
    }
}
